import { formatSize } from '@web-utils/core'
import { services } from '@/config'
import { downloadFiles } from '@/api/file'
import { withContext } from '@/util/core'

export const getFileHtml = message => {
  const media = message.media
  if (!media) {
    return message.content
  }
  return `<div class="file-item">
    <div class="file-item-icon" style="background-image: url('${getFileIcon(media.filename)}');"></div>
    <div class="file-item-wrapper"><p class="file-item-name">${message.content}</p>
        <div class="file-item-footer">
            <p class="file-item-size">${formatSize(media.size)}</p>
            <a class="file-item-download" href="javascript:void(0);" data-id="${media.id}" data-file-name="${
              media.filename
            }">下载</a>
        </div>
    </div>
</div>`
}

export const getHtml = item => {
  if (item.type === 'file') {
    return getFileHtml(item)
  } else if (item.type === 'image') {
    return getPictureHtml(item)
  } else {
    return item.content
  }
}

export const getPictureHtml = message => {
  const media = message.media
  if (!media) {
    return message.content
  }
  return `<img alt="图片" class="chat-image" onerror="this.src = '${require('@/assets/images/error.png')}'" src="${withContext(
    `${services.adminService}sys-file/${media.bucketName}/${media.name}`
  )}">`
}

export const parseMsg = item => {
  if (['file', 'image'].includes(item.type)) {
    return getHtml(item)
  } else {
    return item.content
  }
}

export const getFileIcon = filename => {
  let icon = 'file_blue.png'
  if (/.jpg|.jpeg|.png|.gif|.bmp|.webp/i.test(filename)) {
    icon = 'file_img.png'
  } else if (/.doc|.docx/i.test(filename)) {
    icon = 'file_doc.png'
  } else if (/.ppt|.pptx/i.test(filename)) {
    icon = 'file_ppt.png'
  } else if (/.xls|.xlsx/i.test(filename)) {
    icon = 'file_xls.png'
  } else if (/.exe/i.test(filename)) {
    icon = 'file_exe.png'
  } else if (/.3gp/i.test(filename)) {
    icon = 'file_3gp.png'
  } else if (/.txt/i.test(filename)) {
    icon = 'file_txt.png'
  } else if (/.pdf/i.test(filename)) {
    icon = 'file_pdf.png'
  } else if (/.bat/i.test(filename)) {
    icon = 'file_bat.png'
  } else if (/.zip|.tgz|.tar/i.test(filename)) {
    icon = 'file_zip.png'
  } else if (/.rar/i.test(filename)) {
    icon = 'file_rar.png'
  } else if (/.7z/i.test(filename)) {
    icon = 'file_7z.png'
  } else if (/.ai/i.test(filename)) {
    icon = 'file_ai.png'
  } else if (/.ape/i.test(filename)) {
    icon = 'file_ape.png'
  } else if (/.apk/i.test(filename)) {
    icon = 'file_apk.png'
  } else if (/.avi/i.test(filename)) {
    icon = 'file_avi.png'
  } else if (/.bt/i.test(filename)) {
    icon = 'file_bt.png'
  } else if (/.cab/i.test(filename)) {
    icon = 'file_cab.png'
  } else if (/.chm/i.test(filename)) {
    icon = 'file_chm.png'
  } else if (/.xml|.html|.css|.less|.scss|.js/i.test(filename)) {
    icon = 'file_code.png'
  } else if (/.dmg/i.test(filename)) {
    icon = 'file_dmg.png'
  } else if (/.error/i.test(filename)) {
    icon = 'file_error.png'
  } else if (/.flv/i.test(filename)) {
    icon = 'file_flv.png'
  } else if (/.grey/i.test(filename)) {
    icon = 'file_grey.png'
  } else if (/.img/i.test(filename)) {
    icon = 'file_img.png'
  } else if (/.ipa/i.test(filename)) {
    icon = 'file_ipa.png'
  } else if (/.iso/i.test(filename)) {
    icon = 'file_iso.png'
  } else if (/.log/i.test(filename)) {
    icon = 'file_log.png'
  } else if (/.mov/i.test(filename)) {
    icon = 'file_mov.png'
  } else if (/.mp3/i.test(filename)) {
    icon = 'file_mp3.png'
  } else if (/.mp4/i.test(filename)) {
    icon = 'file_mp4.png'
  } else if (/.msi/i.test(filename)) {
    icon = 'file_msi.png'
  } else if (/.music/i.test(filename)) {
    icon = 'file_music.png'
  } else if (/.psd/i.test(filename)) {
    icon = 'file_psd.png'
  } else if (/.rm/i.test(filename)) {
    icon = 'file_rm.png'
  } else if (/.rmvb/i.test(filename)) {
    icon = 'file_rmvb.png'
  } else if (/.swf/i.test(filename)) {
    icon = 'file_swf.png'
  } else if (/.video/i.test(filename)) {
    icon = 'file_video.png'
  } else if (/.wma/i.test(filename)) {
    icon = 'file_wma.png'
  } else if (/.wmv/i.test(filename)) {
    icon = 'file_wmv.png'
  }
  return `./static/img/fileIcons/${icon}`
}

export const downloadFile = event => {
  const a = event.target
  if (a.classList.contains('file-item-download')) {
    event.preventDefault()
    event.stopPropagation()
    event.stopImmediatePropagation()
    const fileId = a.getAttribute('data-id')
    return downloadFiles([fileId])
  }
}

export const onLoadComplete = img => {
  if (img.complete) {
    return true
  }
  return new Promise(resolve => {
    const url = img.getAttribute('data-src') || img.getAttribute('src')
    const image = new Image()
    image.src = url
    if (image.complete) {
      return resolve(true)
    } else {
      image.onload = () => {
        resolve(true)
      }
      image.onerror = () => {
        resolve(true)
      }
    }
  })
}
